import pandas as pd
import numpy as np

def calculate_z(x,m,s):
    #计算单科标准分，并限制范围四舍五入
    z = ((x-m)/s) * 100 + 500
    if z > 900:
        z = 900
    elif z < 100:
        z = 100
    return round(z)
def calculate_final_score():
    try:
        # 读取数据
        df = pd.read_csv('scores.csv')
        
        # 输入考生序号
        student_id = int(input().strip())
        
        # 检查考生是否存在
        if student_id not in df['id'].values:
            print("考生序号不存在")
            return
        
        # 科目列表
        compulsory_subjects = ['A', 'B', 'C']
        optional_subjects = ['D', 'E', 'F', 'G', 'H', 'I']
        all_subjects = compulsory_subjects + optional_subjects
        
        # 步骤1: 计算每个科目的平均分和标准差
        subject_stats = {}
        for subject in all_subjects:
            # 只取有成绩的考生（忽略NaN）
            subject_data = df[subject].dropna()
            m = subject_data.mean()
            s = subject_data.std()  
            # 避免s=0的情况（如果所有分数相同）
            if s == 0:
                s = 1e-6  # 防止除零
            subject_stats[subject] = {'mean': m, 'std': s}
        
        # 步骤2: 为每个考生计算单科标准分
        z_scores_df = pd.DataFrame(index=df.index, columns=all_subjects)
        for subject in all_subjects:
            m = subject_stats[subject]['mean']
            s = subject_stats[subject]['std']
            # 只为有成绩的科目计算标准分，否则留NaN
            valid_mask = df[subject].notna()
            z_scores_df.loc[valid_mask, subject] = df.loc[valid_mask, subject].apply(
                lambda x: calculate_z(x, m, s))
        
        # 步骤3: 计算每个考生的加权标准分和
        weighted_sums = []
        for idx, row in z_scores_df.iterrows():
            # 必选科目标准分乘以1.5
            comp_sum = 1.5 * (row['A'] + row['B'] + row['C'])
            # 任选科目：只取有成绩的三科，乘以1.0
            optional_scores = row[optional_subjects].dropna()
            opt_sum = optional_scores.sum()  # 权重1.0，所以直接加
            total_sum = comp_sum + opt_sum
            weighted_sums.append(total_sum)
        
        df['weighted_sum'] = weighted_sums
        
        # 步骤4: 计算所有考生加权标准分和的平均值和标准差（总体）
        M = df['weighted_sum'].mean()
        S = df['weighted_sum'].std()
        if S == 0:
            S = 1e-6  # 防止除零
        
        # 步骤5: 计算给定考生的最后得分
        student_row = df[df['id'] == student_id]
        if student_row.empty:
            raise ValueError(f"考生序号 {student_id} 不存在")
        
        sum_weighted = student_row['weighted_sum'].values[0]
        final_score = ((sum_weighted - M) / S) * 100 + 500
        if final_score > 900:
            final_score = 900
        elif final_score < 100:
            final_score = 100
        final_score = round(final_score)
        
        print(final_score)
    
    except FileNotFoundError:
        print("错误：未找到文件 'scores.csv'")
    except Exception as e:
        print(f"发生错误：{e}")

calculate_final_score()